<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>教师测评打分 - IVT绩效考核系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #f5f5f5;
            color: #333;
            padding-bottom: 80px;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 15px 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .back-btn {
            background: none;
            border: none;
            color: white;
            font-size: 18px;
            margin-right: 15px;
            cursor: pointer;
            padding: 5px;
        }

        .header h1 {
            font-size: 18px;
            font-weight: 600;
        }

        .container {
            padding: 20px;
        }

        .teacher-info {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }

        .teacher-info h2 {
            font-size: 20px;
            color: #667eea;
            margin-bottom: 15px;
        }

        .info-item {
            display: flex;
            margin-bottom: 10px;
        }

        .info-label {
            font-size: 14px;
            color: #666;
            min-width: 80px;
        }

        .info-value {
            font-size: 14px;
            color: #333;
            font-weight: 500;
        }

        .evaluation-form {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }

        .form-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            color: #333;
        }

        .indicator-item {
            margin-bottom: 25px;
            padding-bottom: 20px;
            border-bottom: 1px solid #f0f0f0;
        }

        .indicator-item:last-child {
            border-bottom: none;
            margin-bottom: 0;
        }

        .indicator-title {
            font-size: 16px;
            font-weight: 500;
            margin-bottom: 10px;
            color: #333;
        }

        .indicator-content {
            font-size: 14px;
            color: #666;
            line-height: 1.5;
            margin-bottom: 15px;
        }

        .score-section {
            margin-top: 15px;
        }


        .options {
            display: flex;
            gap: 8px;
            flex-wrap: nowrap;
        }

        .option {
            flex: 1;
            min-width: 50px;
            background: #f8f9fa;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            padding: 8px 6px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            user-select: none;
        }

        .option:hover {
            border-color: #667eea;
            background: #f0f2ff;
        }

        .option.selected {
            background: #667eea;
            border-color: #667eea;
            color: white;
        }

        .option-label {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 2px;
        }

        .option-score {
            font-size: 11px;
            color: inherit;
        }

        .option.selected .option-score {
            color: rgba(255, 255, 255, 0.9);
        }

        .submit-section {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background: white;
            padding: 15px 20px;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        }

        .total-score {
            text-align: center;
            margin-bottom: 15px;
            font-size: 16px;
            color: #333;
        }

        .total-value {
            font-size: 24px;
            font-weight: 600;
            color: #667eea;
        }

        .submit-btn {
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            padding: 15px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
        }

        .submit-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .loading {
            display: none;
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .loading.show {
            display: block;
        }

        .error-message {
            background: #fee;
            color: #c33;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            display: none;
        }

        .error-message.show {
            display: block;
        }


    </style>
</head>
<body>
<div class="header">
    <button class="back-btn" onclick="goBack()">‹</button>
    <h1>教师测评打分</h1>
</div>

<div class="container">
    <div class="error-message" id="errorMessage"></div>

    <div class="loading" id="loading">
        正在加载测评内容...
    </div>

    <div class="teacher-info" id="teacherInfo" style="display: none;">
        <h2 id="teacherName"></h2>
    </div>

    <div class="evaluation-form" id="evaluationForm" style="display: none;">
        <div class="form-title">测评指标</div>
        <div id="indicatorsList"></div>
    </div>
</div>

<div class="submit-section" id="submitSection" style="display: none;">
    <div class="total-score">
        总分：<span class="total-value" id="totalScore">0</span>
    </div>
    <button class="submit-btn" id="submitBtn" onclick="submitEvaluation()">
        提交测评
    </button>
</div>

<script>
        let relationId = null;
        let indicators = [];
        let scores = {};

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            initPage();
        });

        function initPage() {
            // 从URL参数获取测评关系ID和其他信息
            const urlParams = new URLSearchParams(window.location.search);
            relationId = urlParams.get('relationId');
            const teacherName = urlParams.get('teacherName');
            const deptName = urlParams.get('deptName');
            const totalScore = urlParams.get('totalScore');

            // 调试信息
            console.log('URL参数调试:', {
                currentUrl: window.location.href,
                relationId: relationId,
                teacherName: teacherName,
                deptName: deptName,
                totalScore: totalScore
            });

            if (!relationId) {
                showError(`参数错误，请重新选择教师。当前URL: ${window.location.href}`);
                return;
            }

            // 显示教师信息
            if (teacherName) {
                try {
                    const decodedTeacherName = decodeURIComponent(teacherName);
                    document.getElementById('teacherName').textContent = decodedTeacherName;
                } catch (error) {
                    console.error('解码教师姓名失败:', error);
                    document.getElementById('teacherName').textContent = teacherName;
                }
            }

            loadEvaluationContent();
        }

        function loadEvaluationContent() {
            document.getElementById('loading').classList.add('show');

            fetch(`/evaluation/api/h5/teacher/content?relationId=${relationId}`)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('loading').classList.remove('show');

                    if (data.success) {
                        indicators = data.data;
                        
                        // 设置教师信息
                        const teacherName = decodeURIComponent(getQueryParam('teacherName') || '');
                        document.getElementById('teacherName').textContent = teacherName;
                        
                        renderIndicators();
                        document.getElementById('teacherInfo').style.display = 'block';
                        document.getElementById('evaluationForm').style.display = 'block';
                        document.getElementById('submitSection').style.display = 'block';
                    } else {
                        showError(data.message || '加载测评内容失败');
                    }
                })
                .catch(error => {
                    document.getElementById('loading').classList.remove('show');
                    console.error('Error:', error);
                    showError('网络错误，请稍后重试');
                });
        }

        function renderIndicators() {
            const container = document.getElementById('indicatorsList');
            container.innerHTML = '';

            indicators.forEach(indicator => {
                const indicatorDiv = document.createElement('div');
                indicatorDiv.className = 'indicator-item';

                // 格式化考核点详解，根据句号进行换行
                const formattedContent = formatDescription(indicator.khdnr || indicator.khdxj || '暂无详细说明');

                indicatorDiv.innerHTML = `
                    <div class="indicator-title">${indicator.khdmc || '未知指标'}</div>
                    <div class="indicator-content">${formattedContent}</div>
                    <div class="score-section">
                        <div class="options" data-question="${indicator.khdbh}">
                            <div class="option" onclick="selectOption('${indicator.khdbh}', 'A', ${indicator.a || 0}, this)">
                                <div class="option-label">A</div>
                                <div class="option-score">${indicator.a || 0}分</div>
                            </div>
                            <div class="option" onclick="selectOption('${indicator.khdbh}', 'B', ${indicator.b || 0}, this)">
                                <div class="option-label">B</div>
                                <div class="option-score">${indicator.b || 0}分</div>
                            </div>
                            <div class="option" onclick="selectOption('${indicator.khdbh}', 'C', ${indicator.c || 0}, this)">
                                <div class="option-label">C</div>
                                <div class="option-score">${indicator.c || 0}分</div>
                            </div>
                            <div class="option" onclick="selectOption('${indicator.khdbh}', 'D', ${indicator.d || 0}, this)">
                                <div class="option-label">D</div>
                                <div class="option-score">${indicator.d || 0}分</div>
                            </div>
                            <div class="option" onclick="selectOption('${indicator.khdbh}', 'E', ${indicator.e || 0}, this)">
                                <div class="option-label">E</div>
                                <div class="option-score">${indicator.e || 0}分</div>
                            </div>
                        </div>
                    </div>
                `;

                container.appendChild(indicatorDiv);

                // 初始化分数
                scores[indicator.khdbh] = 0;
            });

            updateTotalScore();
        }

        // 选择答案
        function selectOption(khdbh, option, score, element) {
            scores[khdbh] = parseInt(score);
            
            console.log('选择选项:', {khdbh, option, score, currentScores: scores});

            // 更新UI
            const options = document.querySelectorAll(`[data-question="${khdbh}"] .option`);
            options.forEach(opt => opt.classList.remove('selected'));
            
            // 如果没有传递element参数，尝试从event获取
            const targetElement = element || event.target;
            if (targetElement) {
                targetElement.classList.add('selected');
            }

            updateTotalScore();
        }

        // 格式化考核点详解
        function formatDescription(desc) {
            return desc.replace(/。/g, '。<br>').trim();
        }

        function updateTotalScore() {
            const total = Object.values(scores).reduce((sum, score) => sum + score, 0);
            document.getElementById('totalScore').textContent = total;
            console.log('更新总分:', total, '当前分数:', scores);
        }

        function submitEvaluation() {
            // 检查是否所有指标都已评分
            const hasEmptyScore = indicators.some(indicator => !scores[indicator.khdbh] || scores[indicator.khdbh] === 0);
            if (hasEmptyScore) {
                alert('请为所有指标评分后再提交');
                return;
            }

            const submitBtn = document.getElementById('submitBtn');
            submitBtn.disabled = true;
            submitBtn.textContent = '提交中...';

            const requestData = {
                relationId: parseInt(relationId),
                scores: Object.entries(scores).map(([khdbh, score]) => ({
                    khdbh: khdbh,
                    score: score
                }))
            };

            fetch('/evaluation/api/h5/teacher/submit', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(requestData)
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 直接跳转到教师测评页面，不显示提示框
                        window.location.href = 'teacher-list.html';
                    } else {
                        alert(data.message || '提交失败，请重试');
                        submitBtn.disabled = false;
                        submitBtn.textContent = '提交测评';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('网络错误，请稍后重试');
                    submitBtn.disabled = false;
                    submitBtn.textContent = '提交测评';
                });
        }

        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.classList.add('show');
        }

        // 获取URL参数
        function getQueryParam(param) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(param);
        }

        function goBack() {
            window.history.back();
        }


</script>
</body>
</html>
